package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.configure.Result;
import com.food.entity.Material;
import com.food.mapper.MaterialMapper;
import com.food.service.MaterialService;
import com.food.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-04
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Resource
    MaterialMapper materialMapper;

    /**
     * 修改材料信息
     * @param material
     * @return
     */
    @Override
    public Result<Material> updateMaterial(Material material) {
        Result<Material> result = new Result<>();
        try {
            if (StringUtils.isEmpty(material.getMaterialId())) {
                //如果ID为空，代表添加，生成ID，插入
                material.setMaterialId(IDGenerator.getUUID("MATERIAL"));
                materialMapper.insert(material);
            } else {
                //如果ID不为空，代表修改
                materialMapper.updateById(material);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 删除及其材料信息
     * @param material
     * @return
     */
    @Override
    public Result<Material> delMaterial(Material material) {
        Result<Material> result = new Result<>();
        try {
            if (StringUtils.isEmpty(material.getMaterialId())) {
                result.setMessage("materialId can't be empty");
                return result;
            }
            materialMapper.deleteById(material.getMaterialId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Material> batchDelMaterial(Material material) {
        Result<Material> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(material.getIds())) {
                String[] ids = material.getIds().split(",");
                for (String id : ids) {
                    materialMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 查询材料--下拉栏
     * @param material
     * @return
     */
    @Override
    public Result<Material> queryMaterial(Material material) {
        Result<Material> result = new Result<>();
        try {
            QueryWrapper<Material> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(material.getMaterialName())) {
                wrapper.lambda().like(Material::getMaterialName, material.getMaterialName());
            }

            List<Material> materials = materialMapper.selectList(wrapper);
            result.setReturnList(materials);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 分页查食材--名字
     * @param material
     * @return
     */
    @Override
    public Result<Material> queryMaterialByPage(Material material) {
        Result<Material> result = new Result<>();

        try {
            if (material.getCurrent() == null || material.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<Material> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(material.getMaterialName())) {
                wrapper.lambda().like(Material::getMaterialName, material.getMaterialName());
            }
            //
            IPage<Material> materials = materialMapper.selectPage(new Page<>(material.getCurrent(), material.getSize()), wrapper);
            result.setReturnPage(materials);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Material> queryMaterialById(Material material) {
        Result<Material> result = new Result<>();
        try {
            Material returnMaterial = materialMapper.selectById(material.getMaterialId());
            result.setReturnObj(returnMaterial);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
